﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace 发送端
{
    public partial class Form1 : Form
    {

        GeLi_ContronalParaMeter.ACPara ACParameter;

        public Form1()
        {
            InitializeComponent();
            ACParameter.temp = 26;
        }

        void SendMessage()
        {
            if (textBox1.Text.Length > 0)
            {
                if (serialPort.IsOpen)
                {
                    string to_send;
                    if (textBox1.Text.Length > 120)
                    {
                        MessageBox.Show("只发送前120个文字");

                        to_send = textBox1.Text.Substring(0, 120);
                    }
                    else
                    {
                        to_send = textBox1.Text;
                    }
                    // serialPort.Write(stringbytes, 0, stringbytes.Length);
                    serialPort.Write(to_send);
                }
                else
                {
                    MessageBox.Show("请打开一个串口");
                }
            }
            else
            {
                MessageBox.Show("发送框为空！");
            }
        }

        private void BtnSend_Click(object sender, EventArgs e)
        {
            SendMessage();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 将所有的端口加载到下拉选项中
            foreach(string portname in SerialPort.GetPortNames())
            {
                comboBox1.Items.Add(portname);
            }
            // 默认选中第一个端口
            if(comboBox1.Items.Count > 0)
            {
                comboBox1.SelectedItem = comboBox1.Items[0];
            }
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(comboBox1.SelectedItem == null)
            {
                return;
            }
            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }
            serialPort.PortName = (string)comboBox1.SelectedItem;
            // 波特率115200
            serialPort.BaudRate = 115200;
            // 数据位8
            serialPort.DataBits = 8;
            // 停止位1
            serialPort.StopBits = StopBits.One;
            // 无校验
            serialPort.Parity = Parity.None;
            // 设置编码
            serialPort.Encoding = Encoding.UTF8;
            try
            {
                serialPort.Open();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void BtnSendAlways_Click(object sender, EventArgs e)
        {
            if(!timer1.Enabled)
            {
                BtnSendAlways.Text = "停止发送";
                timer1.Start();
            }
            else
            {
                BtnSendAlways.Text = "持续发送";
                timer1.Stop();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if(serialPort.IsOpen && textBox1.Text.Length > 0)
                SendMessage();
        }

        private void BtnSendInstrution_Click(object sender, EventArgs e)
        {
            SendInstruction();
        }

        /// <summary>
        /// 发送空调控制指令
        /// </summary>
        public void SendInstruction()
        {
            byte[] commands = GeLi_ContronalParaMeter.Set(ACParameter);
            // 发送ascii码 request
            serialPort.BaseStream.WriteByte(0x05);
            serialPort.Write(commands, 0, commands.Length);
        }


        private void ModeAuto_CheckedChanged(object sender, EventArgs e)
        {
            if(ModeAuto.Checked)
            {
                ACParameter.Mode = GeLi_ContronalParaMeter.Mode.自动;
            }
        }

        

        private void ModeAir_CheckedChanged(object sender, EventArgs e)
        {
            if (ModeAir.Checked)
            {
                ACParameter.Mode = GeLi_ContronalParaMeter.Mode.送风;
            }
        }

        private void ModeCold_CheckedChanged(object sender, EventArgs e)
        {
            if (ModeCold.Checked)
            {
                ACParameter.Mode = GeLi_ContronalParaMeter.Mode.制冷;
            }
        }

        private void ModeDry_CheckedChanged(object sender, EventArgs e)
        {
            if (ModeDry.Checked)
            {
                ACParameter.Mode = GeLi_ContronalParaMeter.Mode.除湿;
            }
        }

        private void ModeHot_CheckedChanged(object sender, EventArgs e)
        {
            if (ModeHot.Checked)
            {
                ACParameter.Mode = GeLi_ContronalParaMeter.Mode.制热;
            }
        }

        private void SweepNone_CheckedChanged(object sender, EventArgs e)
        {
            if (SweepNone.Checked)
            {
                ACParameter.Sweep = GeLi_ContronalParaMeter.Sweep.无扫风;
            }
        }

        private void SweepHorizontal_CheckedChanged(object sender, EventArgs e)
        {
            if (SweepHorizontal.Checked)
            {
                ACParameter.Sweep = GeLi_ContronalParaMeter.Sweep.左右;
            }
        }

        private void SweepVertical_CheckedChanged(object sender, EventArgs e)
        {
            if (SweepVertical.Checked)
            {
                ACParameter.Sweep = GeLi_ContronalParaMeter.Sweep.上下;
            }
        }

        private void SweepAll_CheckedChanged(object sender, EventArgs e)
        {
            if (SweepAll.Checked)
            {
                ACParameter.Sweep = GeLi_ContronalParaMeter.Sweep.上下左右;
            }
        }

        private void LightOn_CheckedChanged(object sender, EventArgs e)
        {
            if (LightOn.Checked)
            {
                ACParameter.Light = GeLi_ContronalParaMeter.Light.亮;
            }
        }

        private void LightOff_CheckedChanged(object sender, EventArgs e)
        {
            if (LightOff.Checked)
            {
                ACParameter.Light = GeLi_ContronalParaMeter.Light.灭;
            }
        }

        private void UltraStrongOff_CheckedChanged(object sender, EventArgs e)
        {
            if (UltraStrongOff.Checked)
            {
                ACParameter.UltraStrong = GeLi_ContronalParaMeter.UltraStrong.普通;
            }
        }

        private void UtralStrongOn_CheckedChanged(object sender, EventArgs e)
        {
            if (UltraStrongOn.Checked)
            {
                ACParameter.UltraStrong = GeLi_ContronalParaMeter.UltraStrong.超强;
            }
        }

        private void BtnPowerOn_Click(object sender, EventArgs e)
        {
            ACParameter.Power = GeLi_ContronalParaMeter.Power.开机;
            SendInstruction();
        }

        private void BtnPowerOff_Click(object sender, EventArgs e)
        {
            ACParameter.Power = GeLi_ContronalParaMeter.Power.关机;
            SendInstruction();
        }

        private void TempPick_ValueChanged(object sender, EventArgs e)
        {
            ACParameter.temp = (int)TempPick.Value;
            SendInstruction();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            SendInstruction();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(timer2.Enabled)
            {
                timer2.Stop();
                button1.Text = "持续发送";
            }
            else
            {
                timer2.Start();
                button1.Text = "停止持续发送";
            }
        }
    }
}
